/**
 * 
 */
package com.zzu.icukangfu.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.core.Controller;
import com.zzu.icukangfu.entity.Patient;
import com.zzu.icukangfu.entity.PatientTM;
import com.zzu.icukangfu.service.PatientService;
import com.zzu.icukangfu.serviceimpl.PatientServiceImpl;

/**
 * @author teng
 * @time下午3:50:24
 * @corporation ttkj
 */
public class PatientController extends Controller {

	// private patient=enhance(MachineServiceImpl.class);
	private PatientService patientService = enhance(PatientServiceImpl.class);

	public void index() {

		renderFreeMarker("/html/index.html");
	}

	/**
	 * 患者登录
	 */

	public void login() {
		Patient patient = this.getModel(Patient.class);
		renderJson(patientService.loginPatient(patient));
	}

	/**
	 * 添加患者界面
	 */
	public void formPatient() {
		renderFreeMarker("/html/addpatient.html");
	}
	/**
	 * patientmessage界面
	 */
	/*
	 * public void patientMessage(){
	 * 
	 * try { String patient_casenum = this.getPara("patient_casenum"); Long
	 * patient_id = patientService.findThePatient(patient_casenum)
	 * .getLong("patient_id");
	 * 
	 * Long patient_id =getParaToLong("patient_id");
	 * 
	 * List<Patient> list = patientService
	 * .findThePatientTrainingMessage(patient_id); Patient patient =
	 * patientService.findThePatient(patient_id); setAttr("patientList", list);
	 * setAttr("patient", patient);
	 * 
	 * 
	 * SimpleDateFormat formatyymmdd = new SimpleDateFormat("yyMMdd");
	 * SimpleDateFormat formatyymmdddate = new SimpleDateFormat("yy:MM:dd");
	 * SimpleDateFormat formatHH= new SimpleDateFormat("HH"); SimpleDateFormat
	 * formatddHH= new SimpleDateFormat("ddHH");
	 * 
	 * 
	 * List<List<Integer>> heartlist=new ArrayList(); List<List<Integer>>
	 * respiratorylist=new ArrayList(); //变量定义 Date nowdate=new Date(); Integer
	 * timerange = -1; Integer twodaterange =-1; Integer viewtime=0; Integer
	 * mintime=30;
	 * 
	 * //当前日期 Integer nowyymmdd=new Integer(formatyymmdd.format(nowdate.getTime()));
	 * Integer nowddHH=new Integer(formatddHH.format(nowdate.getTime())); Integer
	 * nowHH=new Integer(formatHH.format(nowdate.getTime())); //防止为空
	 * if(list.get(0)==null){ list.add(new Patient()); } //表格表示的时间 String
	 * lastdate=formatyymmdd.format(list.get(0).getDate("patient_trainingtime"));
	 * for(Patient pa:list){ System.out.println(pa); //获取患者的康复训练时间 Date
	 * patientdate=pa.getDate("patient_trainingtime");
	 * 
	 * //格式为yymmdd并转化为integer Integer patientyymmdd=new Integer
	 * (formatyymmdd.format(patientdate)); //格式为HH并转化为integer Integer patientHH=new
	 * Integer(formatHH.format(patientdate)); //格式为ddHH并转化为integer Integer
	 * patientddHH=new Integer(formatddHH.format(patientdate));
	 * 
	 * 
	 * 
	 * //由当前时间减去患者时间得到距离现在的时间 timerange=nowyymmdd-patientyymmdd;
	 * 
	 * //如果两天以上跳出循环防止跨天 if(timerange>2)break;
	 * if((nowdate.getTime()-patientdate.getTime())>21600000)break;
	 * System.out.println("timerange"+timerange); //如果同一个小时只取出第一个
	 * if(twodaterange==(nowddHH-patientddHH)) { continue; }
	 * twodaterange=nowddHH-patientddHH; System.out.println("patientHH"+patientHH);
	 * System.out.println("twodaterange"+twodaterange);
	 * 
	 * System.out.println("24-patientHH+nowHH"+(24-patientHH+nowHH)); //不夸天
	 * 3110-3105 if(timerange==0){ viewtime=patientHH+1; } //跨天 0610 01
	 * 
	 * else if(timerange>0){ if((24-patientHH+nowHH)<12){ viewtime=patientHH+1; } }
	 * System.out.println("viewtime"+viewtime); //折线图数据【时间，数据】 List<Integer> l=new
	 * ArrayList<Integer>(); if(mintime>viewtime) { mintime=viewtime; } //添加
	 * l.add(viewtime); l.add(pa.getInt("patient_heartrate")); heartlist.add(l);
	 * l.set(1,pa.getInt("patient_respiratoryrate")); respiratorylist.add(l);
	 * System.out.println("patientHH"+patientHH); System.out.println("nowmm"+nowHH);
	 * System.out.println("heartlist"+heartlist);
	 * 
	 * 
	 * }
	 * 
	 * System.out.println(nowHH); System.out.println(heartlist);
	 * setAttr("heartlist", heartlist); setAttr("respiratorylist", respiratorylist);
	 * setAttr("mintime", mintime); setAttr("now",
	 * formatyymmdddate.format(nowdate));
	 * 
	 * 
	 * 
	 * } catch (Exception e) {
	 * 
	 * e.printStackTrace(); } renderFreeMarker("/html/patientmessage.html"); }
	 */

	/**
	 * patientmessage界面
	 */
	public void patientMessage() {

		try {
			/*
			 * String patient_casenum = this.getPara("patient_casenum"); Long patient_id =
			 * patientService.findThePatient(patient_casenum) .getLong("patient_id");
			 */

			Long patient_id = getParaToLong("patient_id");

			List<Patient> list = patientService.findThePatientTrainingMessage(patient_id);
			Patient patient = patientService.findThePatient(patient_id);
			setAttr("patientList", list);
			setAttr("patient", patient);

			SimpleDateFormat formatyymmdd = new SimpleDateFormat("yyMMdd");
			SimpleDateFormat formatyymmdddate = new SimpleDateFormat("yy:MM:dd");
			SimpleDateFormat formatHH = new SimpleDateFormat("HH");
			SimpleDateFormat formatddHH = new SimpleDateFormat("ddHH");

			List<List<Integer>> heartlist = new ArrayList();
			List<List<Integer>> respiratorylist = new ArrayList();
			// 变量定义
			Date nowdate = new Date();
			Long nowTimeStamp = nowdate.getTime();

			// 当前日期
			Integer nowyymmdd = new Integer(formatyymmdd.format(nowdate.getTime()));
			Integer nowddHH = new Integer(formatddHH.format(nowdate.getTime()));
			Integer nowHH = new Integer(formatHH.format(nowdate.getTime()));
			// 防止为空
			/*
			 * if(list.get(0)==null){ list.add(new Patient()); }
			 */
			// 表格表示的时间
			/*
			 * String
			 * lastdate=formatyymmdd.format(list.get(0).getDate("patient_trainingtime"));
			 */

			// 控制跳出时间
			Integer hour = -1;

			for (Patient pa : list) {

				// 获取患者的康复训练时间
				Date patientdate = pa.getDate("patient_trainingtime");
				Long patientTimeDate = patientdate.getTime();

				Integer patientHour = (int) (nowTimeStamp - patientTimeDate) / (1000 * 3600);

				if (patientHour > 24)
					break;

				if (patientHour.equals(hour)) {
					continue;
				} else {
					hour = patientHour;
				}

				List<Integer> item = new ArrayList<Integer>();

				item.add(new Integer(formatHH.format(patientdate.getTime())));
				item.add(pa.getInt("patient_heartrate"));
				heartlist.add(item);
				System.out.println(item);
				List<Integer> item2 = new ArrayList<Integer>();
				item2.add(new Integer(formatHH.format(patientdate.getTime())));
				item2.add(pa.getInt("patient_respiratoryrate"));
				respiratorylist.add(item2);
				System.out.println("距离目前小时:" + patientHour);
			}

			if (heartlist.isEmpty()) {
				List<Integer> item = new ArrayList<Integer>();
				item.add(0);
				item.add(0);
				heartlist.add(item);
				if (respiratorylist.isEmpty()) {
					respiratorylist.add(item);
				}
			}

			setAttr("heartlist", heartlist);
			setAttr("respiratorylist", respiratorylist);
			setAttr("now", formatyymmdddate.format(nowdate));
			renderJson("list", list);
			// 已添加修改

		} catch (Exception e) {

			e.printStackTrace();
		}
		// renderFreeMarker("/html/patientmessage.html");
	}

	// ----------------------------

	/**
	 * 添加患者
	 */
	public void addPatient() {

		Patient patient = this.getModel(Patient.class);

		patientService.addPatient(patient);
		Patient patientnew = patientService.findThePatient(patient.getStr("patient_casenum"));
		renderJson(patientnew);
	}

	/**
	 * 添加一条患者训练数据 下位机必须发送患者id和机器标识码
	 */
	public void addPatientTrainingMessage() {

		PatientTM patientTM = this.getModel(PatientTM.class);

		renderJson(patientService.addPatientTrainingMessage(patientTM));
	}

	/**
	 * 返回json的training的数据
	 */
	public void findThePatientTrainingMessage() {

		Long patient_id = this.getParaToLong("patient_id");
		renderJson(patientService.findThePatientTrainingMessage(patient_id));
	}

	/**
	 * 
	 */
	public void getTrainingMessage() {

	};

}
